package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 审核流程 控制器
 */
@Controller
// 注意：访问controller的路径可以 / 开头 或 不是/ 开头, 注意要跟管理填写对应的url保持一致
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }


    @RequiresPermissions("system:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix + "add";
    }



    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        return bpmnInfoService.query(qo);
    }

    //新增
    @RequiresPermissions("system:bpmnInfo:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo){
        bpmnInfoService.save(bpmnInfo);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
        bpmnInfoService.update(bpmnInfo);
        return AjaxResult.success();
    }

    // 流程定义的删除 ----- 3 ----
    @RequiresPermissions("system:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }

    // 查看资源文件(下载文件) ： xml 格式  png格式  ----- 2 -----
    @RequiresPermissions("system:bpmnInfo:readResource")
    @RequestMapping("/readResource")
//    @ResponseBody    // 这里不需要JsoN
    public void readResource(String deploymentId, String type, HttpServletResponse response) throws IOException {
        // 从 activiti7 中查询出 xml png 文件，通过input 流方式返回
        InputStream inputStream = bpmnInfoService.readResource(deploymentId, type);
        // 将图片、文件 通过 output 流方式输出到浏览器
        IOUtils.copy(inputStream, response.getOutputStream());

    }

    // 流程图 部署 ------ 1.2 -----
    @RequiresPermissions("system:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path, String bpmnType, String info){
        bpmnInfoService.deploy(path, bpmnType ,info);
        return AjaxResult.success();
    }

    // 流程图 文件上传 -------- 1.1 -----
    @RequiresPermissions("system:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        if (file != null && file.getSize() > 0){
            // 文件后缀判断 只能是 bpmn 后缀
            String originalFilename = file.getOriginalFilename();// 获取文件的原始名称
            // 获取文件后缀名
            String ext = FilenameUtils.getExtension(originalFilename);
            if (ext.toLowerCase().endsWith("bpmn")){
                // 文件保存 --- 思考保存在哪 --
                // 以前在 webapp 文件创建 upload 文件夹并保存上传文件，这套在springboot是不适用的
                // 原因：springboot 后续部署打包成jar，无法进行文件上传
                // 解决方案，在服务器硬盘中设置一个文件夹，配置静态映射路径（ip端口可以直接访问）
                // 怎么获取到配置的静态映射路径？
                String uploadPath = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
                return AjaxResult.success("上传成功",uploadPath);
            }else {
                throw new BaseException("文件必须是bpmn格式");
            }
        }else {
            throw new  BaseException("文件不能为空");
        }
    }
}
